import { StorageService } from '../../../../providers/StorageService';
import { PIC_URL } from '../../../../providers/Config';
import { BasePage } from '../../../../pages/basePage';
import { UserService } from '../../../../providers/UserService';
import { nailerService } from '../../../../providers/nailerService';
import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { EmojiPickerModule } from '@ionic-tools/emoji-picker';
declare var $: any;

/**
 * Generated class for the CommentDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-comment-detail',
  templateUrl: 'comment-detail.html',
})
export class CommentDetailPage extends BasePage{
  picrooturl = '';    //图片地址
  userinfo={}       //用户信息
  commentDetail=[]        //评论详情
  commentDetailList=[]      //评论列表
  five = [1, 2, 3, 4, 5]; //  星级
  remark_id=0;    //从上个页面传来的评论id
  answerContent=''; //店主的回复内容
  toggled: boolean = false;
  emojitext="";
  constructor(public injector: Injector, public userService: UserService, public nailerService: nailerService, public stc:StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    console.log(this.navParams.get("par"));
    this.remark_id=this.navParams.get("list")
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad CommentDetailPage');
  }
  ionViewWillEnter(){
    this.checkShopCommentsAPI();
  }
  dismissCLI(i){
    this.stc.write("back2",this.navParams.get("par"))
    if(i){
      this.viewCtrl.dismiss(i);
    }else{
      this.viewCtrl.dismiss();
    }
    
  }
  // /** 获取评论 */
  async checkShopCommentsAPI() {
    let param={shop_id:this.userinfo['shop_id']};
    let res = await this.userService.checkShopCommentsAPI(param);
    console.log(res);
    if (res.code == 1){
      this.commentDetailList = res.data;
      this.commentDetailList.forEach((e)=>{
          if(e.remark_id == this.remark_id){
            this.commentDetail.push(e);
            console.log(this.commentDetail)
          }
      })
    } else {
      let toast = this.toastCtrl.create({
        message: res.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }
  isArray(obj) {
    return Array.isArray(obj)

  }
  //监听手机键盘是否点击完成按钮
  focusInput(){ 
    if(this.toggled){
      this.toggled=false;
      $(".response").css("bottom","0");
    }
    var idInput=document.getElementById("input");  
    idInput.onkeyup=(event)=>{ 
      if(event.keyCode==13){  
        //执行相应的方法
        this.shopAnswer();  
      }  
    }  
  }  
  //店主回复评论
  async shopAnswer(){
    let param={remark_id:this.remark_id,replay_content:this.emojitext};
    let result=await this.nailerService.shopkeeperReplayAPI(param);
    console.log(param);
    console.log(result)
    if(result.code == 1){
        this.commentDetail=[]        //评论详情
        this.commentDetailList=[]      //评论列表
        this.checkShopCommentsAPI();
        setTimeout(()=>{
          this.dismissCLI("ok")
        },1000)
      
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1000,
        position: 'bottom',
      })
      toast.present();
    }
  }

  //切换当前选中的emoji表情
  　public handleSelection(event) {
      this.emojitext = this.emojitext + " " + event.char;
　　}
  emoji(){
        // this.toggled = !this.toggled;
        // if(this.toggled){
        //   $(".response").css("bottom","314px")
        // }else{
        //   $(".response").css("bottom","0")
        // }
    setTimeout(() => {
      this.toggled = !this.toggled;
      if(this.toggled){
        $(".response").css("bottom","314px")
      }else{
        $(".response").css("bottom","0")
      }
    },100);
  }
  happ(){
    console.log(this.happy("�🏦"))
  }
  happy(str){
   // let str=
   console.log("😍")
    var result = [];
    var list = str.split("");
    for(var i=0;i<list.length;i++){
        if(i != 0){
            result.push(" ");
        }
        var item = list[i];
        var binaryStr = item.charCodeAt().toString(2);
        result.push(binaryStr);
    }   
    return result.join("");
  }
  noo(){
    console.log(this.no("1101100000111101 1101111000001101"));
  }
  no(str){
    var result = [];
    var list = str.split(" ");
    for(var i=0;i<list.length;i++){
         var item = list[i];
         var asciiCode = parseInt(item,2);
         var charValue = String.fromCharCode(asciiCode);
         result.push(charValue);
    }
    return result.join("");
  }
}
